package com.dragon.www.smarthome.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dragon.www.smarthome.R;
import com.dragon.www.smarthome.entity.Room;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by liqp on 2016/3/16.
 */
public class RoomAdapter extends BaseAdapter {

    private List<Room> rooms = new ArrayList<>();
    private Context mcontext;
    private boolean mbedit;
    private HashMap<String, Object> mHasIntoEditModeMap = new HashMap<String, Object>();
    public RoomAdapter(Context context) {
        mcontext = context;
    }

    @Override
    public int getCount() {
        return rooms.size();
    }

    @Override
    public Object getItem(int position) {
        return rooms.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setData(List<Room> rooms) {
        this.rooms = rooms;
        notifyDataSetChanged();
    }

    public void setRoomEditEnable(boolean bEdit){
        mbedit = bEdit;
    }

    public void setRoomEditMap(HashMap<String, Object> map){
        mHasIntoEditModeMap = map;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Room room = rooms.get(position);
        RoomViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new RoomViewHolder();
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.room_view_item, parent, false);
            convertView.setLayoutParams(new AbsListView.LayoutParams((int) (parent.getWidth() / 3), (int) (parent.getHeight() / 2)));// 动态设置item的高度
            viewHolder.iv_room = (ImageView) convertView.findViewById(R.id.iv_room);
            viewHolder.iv_room_edit = (ImageView) convertView.findViewById(R.id.iv_room_edit);
            viewHolder.v_room_edit_mask = (View) convertView.findViewById(R.id.v_room_edit_mask);
            viewHolder.tv_room_name_big = (TextView) convertView.findViewById(R.id.tv_room_name_big);
            viewHolder.tv_room_name_small = (TextView) convertView.findViewById(R.id.tv_room_name_small);
            viewHolder.tv_room_desc = (TextView) convertView.findViewById(R.id.tv_room_desc);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (RoomViewHolder) convertView.getTag();
            convertView.setLayoutParams(new AbsListView.LayoutParams((int) (parent.getWidth() / 3), (int) (parent.getHeight() / 2)));// 动态设置item的高度
        }

        if(mbedit && (position < rooms.size()-1)){
            viewHolder.iv_room_edit.setVisibility(View.VISIBLE);
            viewHolder.v_room_edit_mask.setVisibility(View.VISIBLE);
            if(mHasIntoEditModeMap.containsKey(String.valueOf(position))) {
                viewHolder.iv_room_edit.setImageResource(R.drawable.selected);
            }else{
                viewHolder.iv_room_edit.setImageResource(R.drawable.uncheck);
            }
        }else{
            viewHolder.iv_room_edit.setVisibility(View.GONE);
            viewHolder.v_room_edit_mask.setVisibility(View.GONE);
        }

        String imagePath = room.getImgPath();
        if (!TextUtils.isEmpty(imagePath)) {
            ImageLoader.getInstance().displayImage(imagePath, viewHolder.iv_room);
            viewHolder.iv_room.setVisibility(View.VISIBLE);
            viewHolder.tv_room_name_small.setVisibility(View.VISIBLE);
            viewHolder.tv_room_name_big.setVisibility(View.GONE);
            viewHolder.tv_room_name_small.setText(room.getRoomName());
        } else {
            viewHolder.iv_room.setVisibility(View.GONE);
            viewHolder.tv_room_name_small.setVisibility(View.GONE);
            viewHolder.tv_room_name_big.setVisibility(View.VISIBLE);
            viewHolder.tv_room_name_big.setText(room.getRoomName());
        }

        String decription = room.getRoomDesciption();
        if (!TextUtils.isEmpty(decription)) {
            viewHolder.tv_room_desc.setText(decription);
            viewHolder.tv_room_desc.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tv_room_desc.setVisibility(View.GONE);
        }

        return convertView;
    }

    public class RoomViewHolder {
        public ImageView iv_room;
        public TextView tv_room_name_big;
        public TextView tv_room_name_small;
        public TextView tv_room_desc;
        public ImageView iv_room_edit;
        public View v_room_edit_mask;
    }
}
